<?php

declare(strict_types=1);

namespace App\Resource\Activity\NewUserGift\Task;

use App\Models\MainDB\Coin;
use App\Models\MainDB\Gift;
use App\Models\MainDB\MallProducts;
use Illuminate\Http\Resources\Json\JsonResource;

class RewardResource extends JsonResource
{
    public function toArray($request)
    {
        $this->reward = null;
        $unit = '个';
        if ($this->reward_type === Gift::RELATION_TYPE) {
            $this->reward = $this->gift;
        } elseif ($this->reward_type === MallProducts::RELATION_TYPE) {
            $this->reward = $this->product;
            $unit = '天';
        } elseif ($this->reward_type === Coin::RELATION_TYPE) {
            $this->reward = $this->coin;
        }

        $name = $this->reward->name ?? ($this->reward->title ?? '');
//        if ($this->reward_type === Coin::RELATION_TYPE) {
//            $name .= $this->count;
//        }

        return [
            'reward_id'      => $this->reward->id ?? null,
            'name'           => $name,
            'image'          => $this->reward->picture ?? null,
            'type'           => $this->reward_type ?? null,
            'count'          => $this->count ?? null,
            'unit'           => $unit,
        ];
    }
}
